package houxb.cn;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class FileDecrypt {
    private static final String SECRET_KEY = "My32CharUltraSecuredKeyAa@123456";

    public static void main(String[] args) throws IOException, NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
        byte[] encodedBytes = Files.readAllBytes(Paths.get(args[0]));
        String encodedString = new String(encodedBytes);
        // Base64解码
        byte[] encryptedBytes = Base64.getDecoder().decode(encodedString);

        // AES解密
        Key key = new SecretKeySpec(SECRET_KEY.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);

        Files.write(Paths.get(args[1]), decryptedBytes);
    }
}
